package managedbean.FRM.SupplierInvoice;

import entity.SCM.Supplier;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.SaleLeadBeanLocal;
import session.stateless.SCM.SupplierBeanLocal;

@ManagedBean(name="viewSupplierInvoicesManagerBean")
@ViewScoped
public class ViewSupplierInvoicesManagerBean implements Serializable {
    public ViewSupplierInvoicesManagerBean() {}
    
    @EJB
    private SupplierBeanLocal supplierBeanLocal;
    @EJB
    private SaleLeadBeanLocal saleLeadBeanLocal;
    
    private Supplier selectedSupplier;
    private String statusMessage;
    private boolean isExisting = false;
    private String supplierName;
    
    public void initView(PhaseEvent event){
//        try
//        {
//            if(selectedSupplier!=null)
//            {
//                System.out.println("test initView"+selectedSupplier.getSupplierName()+" ");
//                   this.setIsExisting(true);
//            }
//            else this.setIsExisting(false);
//        }
//        catch (Exception ex)
//        {
//            ex.printStackTrace();            
//        }
    }

    public boolean isIsExisting() {
        return isExisting;
    }

    public void setIsExisting(boolean isExisting) {
        this.isExisting = isExisting;
        
    }

    public String getSupplierName() {
        return supplierName;
    }

    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

    

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public Supplier getSelectedSupplier() {
        return selectedSupplier;
    }

    public void setSelectedSupplier(Supplier selectedSupplier) {
        this.selectedSupplier = selectedSupplier;
    }
    
    public void saveNewDeposit(ActionEvent event)
    {
//        creditAccountBeanLocal.addDeposit(selectedSupplier.getRegistrationNumber(), depositAdd);
//        statusMessage = "New deposit of $" +this.depositAdd+ " has been added Successfully. Your credit limit has been updated to " +supplierBeanLocal.findSupplierById(selectedSupplier.getRegistrationNumber()).getCreditAccount().getCreditLimit()+". "; 
//        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Result: " +statusMessage + "." , "")); 
//        
//        this.setSelectedSupplier(supplierBeanLocal.findSupplierById(selectedSupplier.getRegistrationNumber()));
    }
    
    public void retrieve()
    {
        if (supplierName!=null)
        {
            this.setSelectedSupplier(supplierBeanLocal.findSupplierByName(supplierName));
            System.out.println("retrieve"+this.getSelectedSupplier().getSupplierName()+"   Size"+this.getSelectedSupplier().getSupplierInvoices().get(0).getSupplierInvoiceId());
            this.setIsExisting(true);
        }
    }
    
    public List<String> complete(String query) {  
        List<String> results = new ArrayList<String>();  
        
        List<Supplier> list = supplierBeanLocal.getAllSupplier();
        
        for (int i = 0; i < list.size(); ++i)
        {
            if (list.get(i).getSupplierName().toLowerCase().contains(query.toLowerCase()))
                results.add(list.get(i).getSupplierName());
        }
        
        return results;  
    }
        
}
